package com.dex4d.ui.mine.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dex4d.R;
import com.dex4d.core.AppToolBarBaseActivity;
import com.dex4d.utils.Commons;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by chengyuchun on 2018/11/5.
 */
public class PasswordKeystoreActivity extends AppToolBarBaseActivity implements Commons.BUNDLE_KEYS {
    @BindView(R.id.et_password)
    EditText et_password;

    @BindView(R.id.et_password2)
    EditText et_password2;

    @BindView(R.id.btn_confirm)
    Button btn_confirm;

    @Override
    protected void initToolbar() {
        showTitle(getString(R.string.backup_keystore));
        showBackView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_password_keystore;
    }

    @Override
    protected void initVariables() {
        et_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length() >0 && et_password2.getText().length()>0){
                    btn_confirm.setBackgroundResource(R.drawable.btn_blue_clickable_bg);
                    btn_confirm.setClickable(true);
                }else {
                    btn_confirm.setBackgroundResource(R.drawable.btn_blue_unclick_bg);
                    btn_confirm.setClickable(false);
                }
            }
        });

        et_password2.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length()>0 && et_password.getText().length()>0){
                    btn_confirm.setBackgroundResource(R.drawable.btn_blue_clickable_bg);
                    btn_confirm.setClickable(true);
                }else {
                    btn_confirm.setBackgroundResource(R.drawable.btn_blue_unclick_bg);
                    btn_confirm.setClickable(false);
                }
            }
        });

    }

    @Override
    protected void loadData() {

    }

    @OnClick(R.id.btn_confirm)
    public void onViewClick(){
        String password1 = et_password.getText().toString();
        String password2 = et_password2.getText().toString();
        if(password1.equals(password2)){
            Bundle bundle  = new Bundle();
            bundle.putString(PWD_KEY,password1);
            openActivity(ExportKeystoreActivity.class,bundle);
            finish();
        }else {
            Toast.makeText(this,getString(R.string.password_error),Toast.LENGTH_SHORT).show();
        }
    }
}
